<template>
<div class="news-list">

  <div style="display:flex;align-items: center;justify-content: space-between;margin:0 0 10px;">
      <div style="display:flex;align-items: center;">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="标题">
            <el-input clearable size="small" v-model="formInline.title" placeholder="请输入标题"></el-input>
          </el-form-item>
          <!-- <el-form-item label="分类">
            <el-select clearable size="small" v-model="formInline.classify" placeholder="请选择">
              <el-option
                v-for="item in typeList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item> -->
          <el-form-item label="是否首页显示">
            <el-select clearable size="small" v-model="formInline.isShow" placeholder="请选择">
              <el-option
                v-for="item in [{
                  value: '1',
                  label: '是'
                },{
                  value: '0',
                  label: '否'
                },]"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="状态">
            <el-select clearable size="small" v-model="formInline.state" placeholder="请选择">
              <el-option
                v-for="item in [{
                  value: '1',
                  label: '启用'
                },{
                  value: '0',
                  label: '禁用'
                },]"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button size="small" type="primary" @click="onSubmit">搜索</el-button>
            <el-button size="small" @click="resetSubmit">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div>
        <el-button size="small" type="primary" @click="add">新建故事</el-button>
      </div>
    </div>

  <el-table ref="multipleTable" v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row empty-text="暂无数据">
    <el-table-column label="ID" width="150">
      <template slot-scope="scope">
        {{ scope.row.id }}
      </template>
    </el-table-column>
    <el-table-column label="标题">
      <template slot-scope="scope">
        {{ scope.row.title }}
      </template>
    </el-table-column>
    <el-table-column label="缩略图" width="140">
      <template slot-scope="scope">
        <img :src="scope.row.picUrl" style="width:100px;height:100px" alt="">
      </template>
    </el-table-column>
    <el-table-column label="分类" width="110">
      <template slot-scope="scope">
        <span>{{ scope.row.typeName }}</span>
      </template>
    </el-table-column>
    <el-table-column label="作者" width="110">
      <template slot-scope="scope">
        <span>{{ scope.row.creator }}</span>
      </template>
    </el-table-column>
    <el-table-column label="浏览次数" width="110">
      <template slot-scope="scope">
        <span>{{ scope.row.clickNum }}</span>
      </template>
    </el-table-column>
    <el-table-column label="发布时间" width="200">
      <template slot-scope="scope">
        {{ scope.row.createTime }}
      </template>
    </el-table-column>
    <el-table-column label="是否首页显示" width="150">
      <template slot-scope="scope">
        <el-tag @click="changeTop(scope.row.id, 0)" style="cursor:pointer;" size="small" v-if="scope.row.isShow == 1" type="success" >取消首页显示</el-tag>
        <el-tag @click="changeTop(scope.row.id, 1)" style="cursor:pointer;" size="small" v-else type="danger">设置首页显示</el-tag>
      </template>
    </el-table-column>
    <el-table-column label="状态" width="100">
      <template slot-scope="scope">
        <el-tag style="cursor:pointer;" size="small" v-if="scope.row.state == 0" type="danger">禁用</el-tag>
        <el-tag style="cursor:pointer;" size="small" v-else type="success">启用</el-tag>
      </template>
    </el-table-column>
    <el-table-column fixed="right" label="操作" width="150">
      <template slot-scope="scope">
        <el-button @click="changeState(scope.row, 0)" type="text" style="color:#ff0000;" v-if="scope.row.state == 1">禁用</el-button>
        <el-button @click="changeState(scope.row, 1)" type="text" v-else>启用</el-button>
        <el-divider direction="vertical"></el-divider>
        <el-button @click="edit(scope.row)" type="text">编辑</el-button>
        <el-divider direction="vertical"></el-divider>
        <el-popconfirm
          @onConfirm="del(scope.row)"
          confirm-button-text='确定'
          cancel-button-text='取消'
          icon="el-icon-info"
          icon-color="red"
          title="是否删除本条数据？">
          <el-button  slot="reference" type="text" style="color:#ff0000;">删除</el-button>
        </el-popconfirm>
      </template>
    </el-table-column>
  </el-table>
  <div style="margin:10px 0 0 0;text-align:right;">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageObj.pageNum"
      :page-sizes="[10, 30, 50, 100]"
      :page-size="pageObj.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pageObj.total">
    </el-pagination>
  </div>
</div>
</template>

<script>
import {
  getListApi,
  deleteApi,
  gettypeListApi,
  changeStateApi,
  changeTopApi
} from '@/api/news'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: [],
      listLoading: true,
      currentPage: 1,
      formInline: {
        isShow: '',
        classify: '',
        state: '',
        title: '',
      },
      pageObj: {
        pageSize: 10,
        pageNum: 1,
        total: 0,
      },
      typeList: []
    }
  },
  created() {
    this.fetchData()
    this.getTypeList()
  },
  methods: {
    onSubmit() {
      this.fetchData()
    },
    add() {
      this.$router.push({
        path: '/news/add'
      })
    },
    changeTop(id, isShow) {
      changeTopApi({
        id: id,
        isShow: isShow
      }).then((res) => {
        this.fetchData()
      })
    },
    changeState(row, state) {
      changeStateApi({
        id: row.id,
        state: state
      }).then((res) => {
        this.fetchData()
      })
    },
    getTypeList() {
      gettypeListApi({
        pageSize: 100,
        pageNum: 1,
      }).then((res) => {
        this.typeList = res.data.result.map((r) => {
          return {
            value: r.id,
            label: r.name
          }
        })
      })
    },
    edit(row) {
      this.$router.push({
        path: '/news/edit',
        query: {
          editId: row.id
        }
      })
    },
    del(row) {
      deleteApi({
        ids: row.id
      }).then((res) => {
        this.fetchData()
      })
    },
    resetSubmit() {
      this.formInline.isShow = ''
      this.formInline.state = ''
      this.formInline.title = ''
      this.formInline.typeId = ''
      this.pageObj.pageSize = 10
      this.pageObj.pageNum = 1
      this.pageObj.total = 0
      this.fetchData()
    },
    fetchData() {
      this.listLoading = true
      getListApi({
        isShow: this.formInline.isShow,
        state: this.formInline.state,
        title: this.formInline.title,
        typeId: this.formInline.classify,
        pageSize: this.pageObj.pageSize,
        pageNum: this.pageObj.pageNum,
      }).then(response => {
        this.list = response.data.result
        this.pageObj.total = response.data.total
        this.listLoading = false
      })
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.pageObj.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.pageObj.pageNum = val
      this.fetchData()
    }
  }
}
</script>

<style lang="scss" scoped>
   .news-list{
     padding: 20px;
     .el-form-item{
       margin-bottom: 0;
     }
   }
</style>
